package archsw0904.controller.registration;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import archsw0904.model.ProfileClient;


@WebService
public @Stateless class RegistrationBean implements RegistrationBeanRemote {
	@PersistenceContext
	EntityManager em;

	@EJB
	ProfileClientBeanLocal pcb;
	
	@EJB
	ProfileAdminBeanLocal pab;
	
	@Override
	public void createProfile(String password, String name, String surname,
			String email, String address, String cap, String city,
			String telephone, String state) throws NullMandatoryFieldException {
		/* campi obbligatori compilati
		 * 
		 * validazione formato del contenuto campi (i campi vuoti a null)
		 * - validazione sintassi email
		 * - validazione tipologia stringhe (nominativi e citta' validi no numeri solo ')
		 * - validazione telefono (solo numeri e +, niente barre)
		 * 
		 * unicita' mail
		 * creazione loginId
		 * 
		 * creazione nuovo profilo
		 * 
		 * */
		
		mandatoryNullFieldCheck(password, name, surname, email);
		emailCheck(email);
		nameCheck(name,surname,city, state);
		capCheck(cap);
		phoneCheck(telephone);
		
		pcb.createProfileClient(password, name, surname, email, address, cap, city, telephone, state) ;
	}
	
	private void capCheck(String cap)throws NullMandatoryFieldException{
		Pattern p = Pattern.compile("[0-9]*");
		Matcher m;
		boolean matchFound ;
		if (cap!=null){
			m = p.matcher(cap);
			matchFound = m.matches();
			if (!matchFound)
				throw new NullMandatoryFieldException("cap format not correct "+ cap) ;	
		}	
		
	}
	
	private void phoneCheck(String telephone) throws NullMandatoryFieldException{
		Pattern p = Pattern.compile("\\+{0,1}[0-9]+");
		if (telephone!=null){
			Matcher m = p.matcher(telephone);
			boolean matchFound = m.matches();
			if (!matchFound&&!(telephone.isEmpty()))
				throw new NullMandatoryFieldException("telephone format not correct "+ telephone) ;
		}
	}

	private void nameCheck(String name, String surname, String city, String state) throws NullMandatoryFieldException {
		Pattern p =Pattern.compile(".*[1-9].*");
		Matcher m;
		boolean matchFound ;
		if (name!=null){ 
			m = p.matcher(name);
			matchFound = m.matches();
			if (matchFound)
				throw new NullMandatoryFieldException("name format not correct "+ name) ;	
		}
		if (name!=null){
			m = p.matcher(surname);
			matchFound = m.matches();
			if (matchFound)
				throw new NullMandatoryFieldException("surname format not correct "+ surname) ;
		}
		if (name!=null){	
			m = p.matcher(city);
			matchFound = m.matches();
			if (matchFound&&!(city.isEmpty()))
				throw new NullMandatoryFieldException("city format not correct"+ city) ; 	
		}
		if (name!=null){	
			m = p.matcher(state);
			matchFound = m.matches();
			if (matchFound&&!(state.isEmpty()))
				throw new NullMandatoryFieldException("state format not correct"+ state) ; 	
		}
	}

	private void emailCheck(String email) throws NullMandatoryFieldException {
		Query q = em.createNamedQuery("ProfileUser.findLoginId").setParameter("loginId", email);
		
		try {
			q.getSingleResult();
			throw new NullMandatoryFieldException("mail not correct, it is in use  by an other client") ;
		} catch (NoResultException e) {
			//Ok
		}
		
		Pattern p = Pattern.compile("[a-zA-Z0-9.]+@[a-zA-Z0-9.]+\\.[a-zA-Z]+");
		Matcher m = p.matcher(email);
		boolean matchFound = m.matches();
		if (!matchFound)
			throw new NullMandatoryFieldException("mail format not correct") ;
	}

	private void mandatoryNullFieldCheck(String password, String name,
			String surname, String email) throws NullMandatoryFieldException {
		
		mandatoryNull(password, "password");
		mandatoryNull(name, "name");
		mandatoryNull(surname, "surname");
		mandatoryNull(email, "email");
		
	}
	
	private void mandatoryNull(String field,String name) throws NullMandatoryFieldException{
		if(field==null ) {
			throw new NullMandatoryFieldException(name + " is empty, obligatory field");
		}
		if (field.isEmpty()) throw new NullMandatoryFieldException(name+ " is empty, obligatory field"); 
	}
	
	public ProfileClient getProfileClientById(String loginId){
		Query q = em.createNamedQuery("ProfileUser.findLoginId").setParameter("loginId", loginId);
		ProfileClient user;
		
		try {
			user = (ProfileClient) q.getSingleResult();
		} catch (NoResultException e) {
			return null;
		}
		
		return user;
	}


	@Override
	public void CreateProfileAdmin(String loginId, String password,
			String name, String surname, String email) {
		pab.createProfileAdmin(loginId, password, name, surname, email);
		
	}

}
